define(['verifyInput','simpleTree'],function(){
    var currPageObj={};
    currPageObj.init=function(){
        var self = this;
        queryRolePageList(1);
    	
      //$(window).resize();
    	
        currPageObj.initEvents();
    };

    //绑定事件
    currPageObj.initEvents=function(){
        var self = this;
      //$(window).resize();
    	$(".page-info").on("click","li",function(){
    		var topage = $(this).attr("page");
    		if(topage != "#"){
    			queryRolePageList(topage);
    		}
    	});
    	
    	$("#pageSize").on("change",function(){
    		queryRolePageList(1);
    	});
    	
    	$("#deleteSel").on("click",function(){
        	deleteSel();
    	});
        
        $("#checkAll").click(function(){
    		$(".table_style1 input[type='checkbox']").prop('checked', $("#checkAll").prop('checked'));
    	});
    	
    	$("#searchBtn").click(function() {
    		queryRolePageList(1);
    	});
    	
    	$(".operation .operation_add").click(function() {
    		showRoleAdd();
    	});
    	
    	$(".add .cancel").click(function() {
    		disshowRoleEdit();
    	});
    	
    	$(".add .submit").click(function() {
    		saveOrEditRole();
    	});
    	
    	$(".rightset .cancel").click(function() {
    		disshowRoleMapMenu();
    	});
    	
    	$(".rightset .submit").click(function() {
    		saveRoleMapMenu();
    	});
    };

    return currPageObj;
});

function freshData() {
	var topage = $(".page-info .cur-page").attr("page");
	if(topage != "#" && topage !="undefined"){
		queryRolePageList(topage);
	}
}

var curPage=0;
var totalPage=0;
function queryRolePageList(currentpage) {
	var pageSize = $("#pageSize").val();
	var roleName = $("#searchRoleName").val();
	var data = $.ajax({
		type : 'post',
		url : 'sysmgr/role/getRolePageList.htm',
		data : 'rolename=' + roleName+'&currentpage=' + currentpage+'&pageSize='+pageSize,
		cache : false,
		success : function(data) {
			
			if(data.error){
				return;
			}
			var res =  data.data;
			rolePagelist = res.dataList;
			curPage = res.currentPage;
			totalPage = res.totalPage;
			totalCount = res.totalCount;
			var listString = "";
			$("#pageDataList").empty();
			for(var i=0; i<rolePagelist.length; i++){
				var listItem = rolePagelist[i];
				var rank = parseInt((curPage-1)*10+i*1+1);
				var roleid = listItem.stRoleId;
				var roleName = listItem.stRoleName;
				var nmOrder = listItem.nmOrder;
				if(i % 2 == 1){
					listString+="<tr class='even'>";
				}else{
					listString+=" <tr>";
				}
				listString+="    <td><input name=\"\" type=\"checkbox\" value=\""+roleid+"\" /></td>";
				listString+="    <td >"+rank+"</td>";
				listString+="    <td><span title="+roleName+">"+ roleName+ "</span></td>";
				listString+="    <td><span title="+nmOrder+">"+ nmOrder+ "</span></td>";
				listString+="    <td ><span><a class=\"modify\" onclick=\"showRoleEdit('"+roleid+"')\" href='javascript:;'>编辑</a>&nbsp;&nbsp;<a class=\"modify\" onclick=\"showRoleMapMenu('"+roleid+"','"+roleName+"')\" href='javascript:;'>权限</a>&nbsp;&nbsp;<a class=\"modify\" onclick=\"deleteRole('"+roleid+"')\" href='javascript:;'>删除</a></span></td>";
				listString+=" </tr>";
			}
			$("#pageDataList").append(listString);
			$("#totalCount").html(totalCount);
			setPageInfoPart(parseInt(curPage),parseInt(totalPage));
		},
		error : function() {
		}
	});
}

function mouseOver(tip)
{
	$("#"+tip).css("display","block");
}

function mouseOut(tip)
{
	$("#"+tip).css("display","none");
}
//设置页码
function setPageInfoPart(curPage, totalPage) {
	$(".page-info .page").remove();
	var startPage = curPage - 5;//要显示的起始页码
	var endPage = curPage + 5;
	
	if(startPage<=0){
		startPage = 1;
	}
	if(endPage >= totalPage){
		endPage = totalPage;
	}
	var showPageNum = endPage - startPage+1;//要显示的页码总数
	//添加页码元素
	var lastElem = $(".page-info .pre");
	var count = 0;
	if(startPage>1){
		$(
				"<li page=1 class='page'>1</li>").insertAfter(lastElem);
		lastElem = $(".page-info .page:eq(" + count + ")");
		count++;
	}
	if(curPage - 1>=10){
		$(
				"<li page='#' class=' page '>……</li>").insertAfter(lastElem);
		lastElem = $(".page-info .page:eq(" + count + ")");
		count++;
	}
	for ( var i = count; i < showPageNum+count; i++) {
		if (curPage == startPage) {
			$(
					"<li page=\""+startPage+"\" class=\"page cur-page\">"
							+ startPage + "</li>").insertAfter(lastElem);
		} else {
			$(
					"<li page=\""+startPage+"\" class=\"page\">"
							+ startPage + "</li>").insertAfter(lastElem);
		}
		lastElem = $(".page-info .page:eq(" + i + ")");
		startPage++;
	}
	count = i;
	
	if(totalPage - curPage >=10){
		$(
				"<li page='#' class=' page '>……</li>").insertAfter(lastElem);
		lastElem = $(".page-info .page:eq(" + count + ")");
		count++;
	}
	if(endPage<totalPage){
		$(
				"<li page="+totalPage+" class='page'>"+totalPage+"</li>").insertAfter(lastElem);
		lastElem = $(".page-info .page:eq(" + totalPage + ")");
		count++;
	}
	//设置首页，上一页，下一页，最后一页页码
	if(curPage == 1){
		$(".page-info .first").css("display","none");
		$(".page-info .pre").css("display","none");
	}else{
		$(".page-info .first").css("display","inline-block");
		$(".page-info .pre").css("display","inline-block");
		
		
		$(".page-info .first").attr("page", 1);
		$(".page-info .pre")
				.attr("page", (curPage - 1) > 0 ? (curPage - 1) : 1);
	}
	if(curPage == totalPage){
		
		$(".page-info .next").css("display","none");
		$(".page-info .last").css("display","none");
	}else{
		$(".page-info .next").css("display","inline-block");
		$(".page-info .last").css("display","inline-block");
		$(".page-info .next").attr("page",
				(curPage + 1) > totalPage ? totalPage : (curPage + 1));
		$(".page-info .last").attr("page", totalPage);
	}

}

function showRoleAdd() {
	$(".add .title").text("添加角色");
	
	$(".tan .tan_min").animate({opacity:1,"margin-top":"0"},600);
	$(".add").fadeIn();
}

function showRoleEdit(id) {
	var data = $.ajax({
		type : 'post',
		url : 'sysmgr/role/getRoleEdit.htm',
		data : 'roleid='+id,
		cache : false,
		success : function(data) {
			var res = data.data;
			var roleinfo = res.roleinfo;
			
			$("#stRoleId").val(roleinfo.stRoleId);
			$("#stRoleName").val(roleinfo.stRoleName);
			$("#nmOrder").val(roleinfo.nmOrder);
			
			$(".add .title").text("修改角色");
			$(".tan .tan_min").animate({opacity:1,"margin-top":"0"},600);
			$(".add").fadeIn();
		},
		error : function() {
		}
	});
	
}

function disshowRoleEdit() {
	$(".tan .tan_min").animate({opacity:1,"margin-top":"-500"},600);
	$(".add").fadeOut();
	$(".add .text").val("");
}

function saveOrEditRole(){
	if(checkInput()){
		var roleId = $("#stRoleId").val().trim();
		var roleName = $("#stRoleName").val().trim();
		var nmOrder = $("#nmOrder").val().trim();
		var data = $.ajax({
			type : 'post',
			url : 'sysmgr/role/saveOrEditRole.htm',
			data : 'roleId=' + roleId+'&roleName=' + roleName+'&nmOrder=' + nmOrder,
			cache : false,
			success : function(data) {
				alert(data.msg);
				freshData();
				disshowRoleEdit();
			},
			error : function() {
			}
		});
	}
	
}

function deleteRole(ids){
	if(confirm("确定要删除吗？")){
		var data = $.ajax({
			type : 'post',
			url : 'sysmgr/role/deleteRole.htm',
			data : 'roleId=' + ids,
			cache : false,
			success : function(data) {
				alert(data.msg);
				freshData();
			},
			error : function() {
			}
		});
	}
	
}

function showRoleMapMenu(id,name){
	
	var data = $.ajax({
		type : 'post',
		url : 'sysmgr/role/getRoleMapMenu.htm',
		data : 'roleid='+id,
		cache : false,
		success : function(data) {
			var res = data.data;
			var rolemaplist = res.roleMap;
			var menulist = res.allmenus;
			
			$("#setRoleId").val(id);
			$("#showRoleName").html(name);
			
			var listString = "<ul>";
			$.each(JSON.parse(menulist),function(i,e){
				var selected = false;
				for(var k=0; k<rolemaplist.length; k++){
					if(rolemaplist[k].stMenuId == e.id){
						selected = true;
						break;
					}
				}
				
				if(e.childs && e.childs.length>0){
					if(selected){
						listString +="<li class=\"folder open\"><input type=\"checkbox\" value=\""+e.id+"\" checked> <a href=\"javascript:;\" >"+e.name+"</a></li>";
					}
					else{
						listString +="<li class=\"folder open\"><input type=\"checkbox\" value=\""+e.id+"\"> <a href=\"javascript:;\" >"+e.name+"</a></li>";
					}
					listString +=" <ul show=\"true\">";
					
					$.each(e.childs,function(ci,ce){
						var childselected = false;
						for(var k=0; k<rolemaplist.length; k++){
							if(rolemaplist[k].stMenuId == ce.id){
								childselected = true;
								break;
							}
						}
						
						if(childselected){
							listString +="<li><input type=\"checkbox\" value=\""+ce.id+"\" checked><a href=\"javascript:;\" >"+ce.name+"</a></li>";
						}
						else{
							listString +="<li><input type=\"checkbox\" value=\""+ce.id+"\"><a href=\"javascript:;\" >"+ce.name+"</a></li>";
						}
						
					});
					
					listString +="</ul>";
				}
				else{
					if(selected){
						listString +="<li><input type=\"checkbox\" value=\""+e.id+"\" checked> <a href=\"javascript:;\" >"+e.name+"</a></li>";
					}
					else{
						listString +="<li><input type=\"checkbox\" value=\""+e.id+"\"> <a href=\"javascript:;\" >"+e.name+"</a></li>";
					}
				}
			});
			listString +="</ul>";
			$(".rightset .st_tree").empty();
			$(".rightset .st_tree").append(listString);
			
			$(".tan .tan_min").animate({opacity:1,"margin-top":"0"},600);
			$(".rightset").fadeIn();
		},
		error : function() {
		}
	});
}

function disshowRoleMapMenu() {
	$(".tan .tan_min").animate({opacity:1,"margin-top":"-500"},600);
	$(".rightset").fadeOut();
	$(".rightset checkbox").val("");
}

function saveRoleMapMenu(){
	var roleId = $("#setRoleId").val();
	var checkArr = [];
    $(".rightset").find(':checkbox:checked').each(function(){
        var id = $(this).val();
        checkArr.push(id);
    });
    
	var data = $.ajax({
		type : 'post',
		url : 'sysmgr/role/saveRoleMapMenus.htm',
		data : 'roleId=' + roleId+'&menumaps=' + checkArr.join(','),
		cache : false,
		success : function(data) {
			alert(data.msg);
			disshowRoleMapMenu();
		},
		error : function() {
		}
	});
}

function checkInput(){
	var roleName = $("#stRoleName").val();
	
	if(roleName.trim() == ""){
		alert("请输入角色名称！");
		$("#stRoleName").focus();
		return false;
	}
	
	var nmOrder = $("#nmOrder").val();
	if(nmOrder.trim() != ""){
		if(isNaN(nmOrder)){
			alert("请输入数字！");
			$("#nmOrder").focus();
			return false;
		}
	}
	return true;
}
